<?php
declare (strict_types = 1);

namespace app\enterprise\controller;

use app\enterprise\model\course\Courseware;
use app\enterprise\model\disclose\Disclose;
use app\enterprise\model\train\Train;
use app\enterprise\model\staff\Staff;
use app\enterprise\model\plan\Plan;
use app\enterprise\model\User;

class Index extends Enterprise
{
    public function index()
    {
        return '您好！这是一个[enterprise]示例应用';
    }


    public function statistics()
    {

        // 如果是总公司, 显示所有的, 否则现在当前登录用的
        $role = $this->adminInfo['role'];

        $where = [];
        $where[] = ['status', '=', 1];
        if ($role == 1) {

        } else {
            $where[] = ['creator_id', '=', $this->adminId];
        }

        // 线上培训
        $plans = [];
        /*
        $plans['today'] = Plan::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('today 00:00:00'),
                strtotime('today 23:59:59')
            ])->count();

        $plans['yesterday'] = Plan::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('yesterday 00:00:00'),
                strtotime('yesterday 23:59:59')
            ])->count();
        */

        $plans['total_all'] = Plan::where($where)
//            ->where('creator_id',$this->adminId)
            ->count();

        $plans['current_month'] = Plan::where($where)
//            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime(date('Y-m-01')),
                strtotime('first day of next month') - 1
            ])->count();

        // 线下培训
        $trains = [];
        /*
        $trains['today'] = Train::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('today 00:00:00'),
                strtotime('today 23:59:59')
            ])->count();

        $trains['yesterday'] = Train::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('yesterday 00:00:00'),
                strtotime('yesterday 23:59:59')
            ])->count();
        */
        $trains['total_all'] = Train::where($where)
//            ->where('creator_id',$this->adminId)
            ->count();

        $trains['current_month'] = Train::where($where)
//            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime(date('Y-m-01')),
                strtotime('first day of next month') - 1
            ])->count();


        // 交底
        $disclose = [];
        /*
        $disclose['today'] = Disclose::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('today 00:00:00'),
                strtotime('today 23:59:59')
            ])->count();

        $disclose['yesterday'] = Disclose::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('yesterday 00:00:00'),
                strtotime('yesterday 23:59:59')
            ])->count();
        */
        $disclose['total_all'] = Disclose::where($where)
//            ->where('creator_id',$this->adminId)
            ->count();

        $disclose['current_month'] = Disclose::where($where)
//            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime(date('Y-m-01')),
                strtotime('first day of next month') - 1
            ])->count();

        // 学员
        $staffs = [];
        /*
        $staffs['today'] = Staff::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('today 00:00:00'),
                strtotime('today 23:59:59')
            ])->count();

        $staffs['yesterday'] = Staff::where('status',1)
            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime('yesterday 00:00:00'),
                strtotime('yesterday 23:59:59')
            ])->count();
        */
        $staffs['total_all'] = Staff::where($where)
//            ->where('creator_id',$this->adminId)
            ->count();

        $staffs['current_month'] = Staff::where($where)
//            ->where('creator_id',$this->adminId)
            ->whereBetween('create_time', [
                strtotime(date('Y-m-01')),
                strtotime('first day of next month') - 1
            ])->count();

        return app('json')->success([
                'data' => [
                    [
                        'title' => '培训次数',
//                        'date' => '今日',
//                        'today' => $plans['today'],
//                        'yesterday' => $plans['yesterday'],
                        'date' => '总计',
                        'total_all' => $plans['total_all'],
                        'total_name' => '本月新增',
                        'total' => $plans['current_month'],
                    ], [
                        'title' => '线下培训次数',
//                        'date' => '今日',
//                        'today' => $trains['today'],
//                        'yesterday' => $trains['yesterday'],
                        'date' => '总计',
                        'total_all' => $trains['total_all'],
                        'total_name' => '本月新增',
                        'total' => $trains['current_month'],
                    ],[
                        'title' => '交底数量',
//                        'date' => '今日',
//                        'today' => $disclose['today'],
//                        'yesterday' => $disclose['yesterday'],
                        'date' => '总计',
                        'total_all' => $disclose['total_all'],
                        'total_name' => '本月新增',
                        'total' => $disclose['current_month'],
                    ], [
                        'title' => '学员总数',
//                        'date' => '今日',
//                        'today' => $staffs['today'],
//                        'yesterday' => $staffs['yesterday'],
                        'date' => '总计',
                        'total_all' => $staffs['total_all'],
                        'total_name' => '本月新增',
                        'total' => $staffs['current_month'],
                    ],
                ]]
        );

    }

    public function getCompanyName()
    {
        $company_name = $this->adminInfo['company_name'];
        return app('json')->success(
            ['company_name' => $company_name]
        );
    }

    public function changePassword()
    {

        $pwd = input('pwd', '');
        $new_pwd = input('new_pwd', '');
        $conf_pwd = input('conf_pwd', '');

        if ( empty($pwd) ) {
            return app('json')->fail('原始密码不能为空');
        }

        if ( empty($new_pwd) || empty($conf_pwd)) {
            return app('json')->fail('新密码或确认新密码不能为空');
        }

        if ( $new_pwd != $conf_pwd ) {
            return app('json')->fail('两次密码不一致');
        }

        $user_info = $this->adminInfo;

        if ( empty($user_info) ) {
            return app('json')->fail('账户不存在');
        }

        if ($user_info['password'] != md5($pwd)) {
            return app('json')->fail('原始密码不正确，请重新输入');
        }

        $user_model = new User();
        $user_model->editPwd(['password' => md5($new_pwd), 'id' => $user_info['id']]);
        return app('json')->success('操作成功');
    }
}
